package com.edais.service.imp;

import com.edais.dao.TurnOutDao;
import com.edais.service.EnjoyService;
import com.lb.sssq.util.CommonUtil;
import com.lb.sssq.util.Consts;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultDTO;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service

@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class EnjoyServiceImp implements EnjoyService {
	private static final Log logger = LogFactory.getLog(EnjoyServiceImp.class);
	
//	@Autowired
//	private WeixinEnjoyDao weixinEnjoyDao;

	@Autowired
	private TurnOutDao turnOutdao;
	
//	@Autowired
//	private UserService userService;
	
	/**
	 * 查询债券列表（原优享计划）
	 * @param map
	 * @return String
	 * @throws Exception
	 */
	@Override
	public ResultDTO queryEnjoyCreditList(Map<String, String> map)
			throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		/**
		 * 调用核心接口 E390003查询债券列表
		 * */
		String transCode = "E390003";
		Map<String, Object> queryCoreMap = new HashMap<String,Object>();
		queryCoreMap.put("userId", map.get("userId"));
		queryCoreMap.put("orderId", map.get("orderId"));
		queryCoreMap.put("current", map.get("current"));
		queryCoreMap.put("pageSize", map.get("pageSize"));
		
		String jsonStr = CommonUtil.constructJsonStr(transCode, map.get("userId"),
				queryCoreMap);
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		if(Consts.SUCCESS_CODE.equals(jsonObject.get("rescode"))){
			ResultDTO resultDTO = new ResultDTO(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),ResultCode.SUCCESS.getType(),
					Integer.parseInt(map.get("pageSize")),jsonObject.getInt("totalNum"),jsonObject.getJSONArray("list"));
			return resultDTO;
		} else {
			logger.info("=====E390003核心接口调用异常=====");
			logger.info("rescode：" + jsonObject.getString("rescode") + "；  E390003核心接口调用异常");
			ResultDTO resultDTO = new ResultDTO(ResultCode.FAILED);
			return resultDTO;
		}
	}
	
	
	/**
	 * 查询活期投资详情（原优享计划）
	 * @param map
	 * @return String
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> queryEnjoyBuyDetail(Map<String, String> paramsMap) throws Exception {
		
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		/**
		 * 调用核心接口 E390012查询投资详情
		 * */
		String transCode = "E390012";
		Map<String, Object> queryCoreMap = new HashMap<String,Object>();
		queryCoreMap.put("productId", paramsMap.get("productId"));
		queryCoreMap.put("userId", paramsMap.get("userId"));
		
		String jsonStr = CommonUtil.constructJsonStr(transCode, paramsMap.get("userId").toString(),
				queryCoreMap);
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		if(Consts.SUCCESS_CODE.equals(jsonObject.get("rescode"))){
			rtnMap.put("enjoyBuyDetail", jsonObject.get("list"));

		} else {
			logger.debug("=====E390012核心接口调用异常=====");
	    	rtnMap.put("enjoyBuyDetail", null);
		}
		return rtnMap;
	}
	
	/**
	 * 查询标的列表
	 * @param map
	 * @return String
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> queryEnjoyLoanList(Map<String, Object> paramsMap) throws Exception {
		
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		/**
		 * 调用核心接口 E390016查询投资详情
		 * */
		String transCode = "E390016";
		Map<String, Object> queryCoreMap = new HashMap<String,Object>();
		queryCoreMap.put("userId", paramsMap.get("userId"));
		queryCoreMap.put("current", paramsMap.get("current"));
		queryCoreMap.put("pageSize", paramsMap.get("pageSize"));
		
		String jsonStr = CommonUtil.constructJsonStr(transCode, paramsMap.get("userId").toString(),
				queryCoreMap);
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		if(Consts.SUCCESS_CODE.equals(jsonObject.get("rescode"))){
			rtnMap.put("list", jsonObject.get("list"));
			rtnMap.put("listSize", jsonObject.get("listSize"));
			rtnMap.put("totalNum", jsonObject.get("totalNum"));
			rtnMap.put("rescode", jsonObject.get("rescode"));
			rtnMap.put("resmsg", "查询成功！");
		} else {
			logger.debug("=====E390016核心接口调用异常=====");
			rtnMap.put("listSize", 0);
	    	rtnMap.put("totalNum", 0);
	    	rtnMap.put("list", new ArrayList());
	    	rtnMap.put("rescode", jsonObject.get("rescode"));
			rtnMap.put("resmsg", "查询失败！");
		}
		return rtnMap;
	}
	
	/***
	 * 优享计划我的账户增加
	 * by fanping
	 */
	@Override
	public Map<String, String> getEnjoyByNumAmIc(String userId)throws Exception {	
		 Map<String,String> resultMap=new HashMap<String,String>();
		
		String transCode = "E390001";
		Map<String, Object> queryCoreMap = new HashMap<String, Object>();
		queryCoreMap.put("userId", userId);
		queryCoreMap.put("productId","107");

		String jsonStr = CommonUtil.constructJsonStr(transCode,
				userId + "", queryCoreMap);
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		String totalAssets = "0";
		String investAmount = "0";
		String yesterdayIncome="0";
		if (Consts.SUCCESS_CODE.equals(jsonObject.get("rescode"))) {
		JSONObject jsonIndex = jsonObject.getJSONObject("indexDetail");
//			totalAssets= jsonIndex.getString("totalAssets") == null ? "0": jsonIndex.getString("totalAssets");
			investAmount = jsonIndex.getString("investAmount") == null ? "0": jsonIndex.getString("investAmount");
			yesterdayIncome = jsonIndex.getString("yesterdayIncome") == null ? "0": jsonIndex.getString("yesterdayIncome");
		} 
//		 resultMap.put("enjoyPlanAmount", totalAssets);//优享计划资产
		 resultMap.put("enjoyPlanAmount", investAmount);//申请中金额
		 resultMap.put("enjoyYesterdayIncome", yesterdayIncome);//（优享）昨日收益
		 //调javaservice的当前抵用券总数
		 Map<String, String> map=new HashMap<String,String>();
		 map.put("userId", userId);
		 map.put("couponType", "107");
		 map.put("status", "0");
		 String curHelpNub=turnOutdao.getCouponListSize(map);
		 Map<String,String> isValidMap=turnOutdao.getIsValid();
		 if(isValidMap==null){
			 isValidMap=new HashMap<String,String>();
		 }
		 resultMap.putAll(isValidMap);
		 if(StringUtils.isBlank(curHelpNub)){
			 curHelpNub="0";
		 }
		resultMap.put("curHelpNub", curHelpNub);//
		return resultMap;
	}
}
